#include "StimObj.h"


CStimObj::CStimObj(const enum Stims::ObjType& eObjType_)
    :
_eObjType(eObjType_)
{
}

CStimObj::~CStimObj(void)
{
    glDeleteLists(_uObj,1);
}


void CStimObj::InitObj()
{
    // pre-generate one instance of the object and store in display list
    GLUquadricObj* q  = gluNewQuadric();
    GLUnurbsObj* nobs = gluNewNurbsRenderer ();
    gluQuadricDrawStyle(q, GLU_FILL);
    gluQuadricNormals(q, GLU_SMOOTH);

    // if (_bTexture)
    //     gluQuadricTexture(q, GL_TRUE);

    _uObj=glGenLists(1);
    glNewList(_uObj,GL_COMPILE);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_MULTISAMPLE);

    switch (_eObjType)
    {
    case Stims::GLpoint:
        glPointSize(_fPointSize);
        glColor3dv(&_vPointColor[0]);

        glBegin(GL_POINTS);
            glVertex3i(0,0,0);
        glEnd();
        break;

    case Stims::Sphere:
        gluBeginSurface(nobs);
        glColor3dv(&_vSphereColor[0]);        gluSphere (q, _dSphereRadius, 20, 20);        gluEndSurface(nobs);
        break;

    case Stims::Disc:
        glColor3dv(&_vDiscColor[0]);        // draw with both orientations so that disc is visible from both directions        // draw for view from above        glPushMatrix();
        glRotated(-90,1,0,0);
        gluBeginSurface(nobs);
        gluDisk(q ,0.,_dDiscRadius,20,1);
        gluEndSurface(nobs);
        glPopMatrix();

        // draw for view from below
        glPushMatrix();
        glRotated( 90,1,0,0);
        gluBeginSurface(nobs);
        gluDisk(q ,0.,_dDiscRadius,20,1);    
        gluEndSurface(nobs);
        glPopMatrix();
        break;

    case Stims::Post:
        glPushMatrix();
        glRotated(-90,1,0,0);
        glColor3dv(&_vPostColor[0]);
        gluBeginSurface(nobs);
        gluCylinder (q, _dPostRadius , _dPostRadius , _dPostHeight, 20, 20);
        gluEndSurface(nobs);
        glPopMatrix();
        break;

    case Stims::PostSphere:
        // Post
        glPushMatrix();
        glRotated(-90,1,0,0);
        glColor3dv(&_vPostColor[0]);
        gluBeginSurface(nobs);
        gluCylinder (q, _dPostRadius , _dPostRadius , _dPostHeight, 20, 20);
        gluEndSurface(nobs);
        glPopMatrix();

        // Sphere
        glPushMatrix();        glTranslated(0.,_dPostHeight,0.);
        gluBeginSurface(nobs);
        glColor3dv(&_vSphereColor[0]);        gluSphere (q, _dSphereRadius, 20, 20);        gluEndSurface(nobs);
        glPopMatrix();
        break;

    default:
        throw std::exception("StimObj::InitObj(): Init for this Object Type not implemented");
        break;
    }

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_MULTISAMPLE);

    glEndList();    gluDeleteQuadric (q);
    gluDeleteNurbsRenderer (nobs);
}